﻿// -----------------------------------------------------------------------
// <copyright file="Quantization.cs" company="FCIS">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace LastVersionOfIpPackage.Operations
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Quantization
    {
        /// <summary>
        /// static object of this class to use this without create object
        /// </summary>
        private static Quantization quantization = new Quantization();

        /// <summary>
        /// Gets or sets the quantization1.
        /// </summary>
        /// <value>
        /// The quantization1.
        /// </value>
        public static Quantization Quantization1
        {
            get { return Quantization.quantization; }
            set { Quantization.quantization = value; }
        }

        /// <summary>
        /// Quantizations the image.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="mask">The mask.</param>
        /// <returns>
        /// The new image
        /// </returns>
        public Bitmap QuantizationImg(Bitmap image, int mask)
        {
            Color[,] bufferOfImage = BufferOfImage.Buffer.GetBuffer(image);
            Bitmap newimg = new Bitmap(bufferOfImage.GetLength(0), bufferOfImage.GetLength(1));
            try
            {
                int width = bufferOfImage.GetLength(0);
                int hight = bufferOfImage.GetLength(1);
                for (int i = 0; i < hight; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        byte red = (byte)(Convert.ToInt32(bufferOfImage[j, i].R) & mask);
                        byte green = (byte)(Convert.ToInt32(bufferOfImage[j, i].G) & mask);
                        byte blue = (byte)(Convert.ToInt32(bufferOfImage[j, i].B) & mask);
                        byte alpa = (byte)(Convert.ToInt32(bufferOfImage[j, i].A) & mask);
                        newimg.SetPixel(j, i, Color.FromArgb(alpa, red, green, blue));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw ex;
            }

            return newimg;
        }
    }
}
